package com.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.delegate.LogSicknessDelegate;
import com.hronline.R;
import com.models.Absence;
import com.models.LogSickness;

public class LogSicknessAdapater extends BaseAdapter {
	
	private Context context;
	private LogSickness logSickness;
	private LayoutInflater inflater;
	private SimpleDateFormat formatter;
	private LogSicknessDelegate delegate;
	

	public LogSicknessAdapater(Context context, LogSicknessDelegate delegate, LogSickness logSickness){
		
		super();
		
		this.context		=	context;
		this.logSickness	=	logSickness;
		this.inflater		=	(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.delegate		=	delegate;
		formatter			=	new SimpleDateFormat("dd MMM");
		
	}
	
	public void setLogSickness(LogSickness logSickness){
		this.logSickness	=	logSickness;
	}
	
	private String convertDateToDisplayString(Date date){
		
		SimpleDateFormat	formatter	=	new SimpleDateFormat("EEE dd MMM HH:mm");
		
		return formatter.format(date);
	}
	
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(this.logSickness.getArrAlreadyAbsence() != null && this.logSickness.getArrAlreadyAbsence().size() > 0){
			return 3 + this.logSickness.getArrAlreadyAbsence().size();
		}
		
		return 2;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		switch (position) {
			case 0:
				return createHolidayYearSection();
				
			case 1:
				return createDetailSection();
				
			case 2:
				return createSectionHeaderForAbsence(); 
	
			default:
				return createAbsenceInYear(position, convertView, parent);
		}
	}
	
	/* this method is used to create holiday year section */
	private View createHolidayYearSection(){

		View view	=	this.inflater.inflate(R.layout.holiday_section_item, null);
		
		TextView txtYearIdValue		=	(TextView)view.findViewById(R.id.txtYearValue);
		TextView txtStartDateLabel	=	(TextView)view.findViewById(R.id.txtStartDateLabel);
		TextView txtStartDateValue	=	(TextView)view.findViewById(R.id.txtStartDateValue);
		TextView txtEndDateLabel	=	(TextView)view.findViewById(R.id.txtEndDateLabel);
		TextView txtEndDateValue	=	(TextView)view.findViewById(R.id.txtEndDateValue);
		TextView txtReasonLabel		=	(TextView)view.findViewById(R.id.txtReasonLabel);
		TextView txtReasonValue		=	(TextView)view.findViewById(R.id.txtReasonValue);
		
		View holidayRow		=	view.findViewById(R.id.holidayYearRow);
		View startDateRow	=	view.findViewById(R.id.startDateRow);
		View endDateRow		=	view.findViewById(R.id.endDateRow);
		
		txtYearIdValue.setText(this.logSickness.getHolidayYear());
		txtStartDateLabel.setText(this.context.getString(R.string.text_log_sickness_start_date));
		txtStartDateValue.setText(convertDateToDisplayString(this.logSickness.getStartDate()));
		txtEndDateLabel.setText(this.context.getString(R.string.text_log_sickness_end_date));
		if(this.logSickness.getEndDate() == null){
			txtEndDateValue.setText("");
		}
		else{
			txtEndDateValue.setText(convertDateToDisplayString(this.logSickness.getEndDate()));
		}
		txtReasonLabel.setText(this.context.getString(R.string.text_log_sickness_reason));
		txtReasonValue.setText(this.logSickness.getDays());
		
		/* hanlde when user click on row */
		holidayRow.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				delegate.didClickHolidayYearRow();
			}
		});
		
		startDateRow.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				delegate.didClickStartDateRow();
			}
		});
		
		endDateRow.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				delegate.didClickEndDateRow();
			}
		});
		
		return view;
		
	}
	
	/* this method is used to create detail section */
	private View createDetailSection(){
		
		View view	=	this.inflater.inflate(R.layout.detail_row_item, null);
		
		return view;
		
	}
	
	/* this method is used to create section header for absence section */
	private View createSectionHeaderForAbsence(){
		
		View view	=	this.inflater.inflate(R.layout.section_header, null);
		view.setBackgroundDrawable(null);
		
		TextView txtHeader	=	(TextView)view.findViewById(R.id.txtHeaderTitle);
		txtHeader.setText(this.context.getString(R.string.text_log_sickness_absences_header));
		txtHeader.setTextColor(Color.BLACK);
		txtHeader.setTextSize(13);
		
		return view;
	}
	
	
	/* this method is used to create absence in year section */
	private View createAbsenceInYear(int position, View convertView, ViewGroup parent){
		int index 		= position - 3;	
		
		View view	=	this.inflater.inflate(R.layout.absence_row_item, parent, false);
//		Resources r = this.context.getResources();
//		float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 45, r.getDisplayMetrics());
//		view.setMinimumHeight((int)px);
		View parentLayout	=	view.findViewById(R.id.parentLayout);
		if (index == 0) {
			parentLayout.setBackgroundDrawable(this.context.getResources().getDrawable(R.drawable.first_row_group_background));
		}
		else if (index == this.logSickness.getArrAlreadyAbsence().size() - 1){
			parentLayout.setBackgroundDrawable(this.context.getResources().getDrawable(R.drawable.last_row_group_background));
		}
		else{
			parentLayout.setBackgroundDrawable(this.context.getResources().getDrawable(R.drawable.middle_row_group_background));
		}
		
		TextView txtAbsenceName	=	(TextView)view.findViewById(R.id.txtAbsenceName);
		TextView txtAbsenceDay	=	(TextView)view.findViewById(R.id.txtAbsenceDays);
		
		Absence item	=	this.logSickness.getArrAlreadyAbsence().get(index);
		SimpleDateFormat dateFormatter	=	new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
		
		try {
			
			Date startDate 	= null;
			Date endDate	= null;
			String strStart	=	"";
			String strEnd	=	"";
			
			if(!item.getStartDate().equalsIgnoreCase("null")){
				startDate	=	dateFormatter.parse(item.getStartDate());
				strStart	=	formatter.format(startDate);
			}
			if(!item.getEndDate().equalsIgnoreCase("null")){
				endDate	=	dateFormatter.parse(item.getEndDate());
				strEnd	=	formatter.format(endDate);
			}
			
			String holiday	=	String.format("%s - %s", strStart, strEnd);
			
			txtAbsenceName.setText(holiday);
			txtAbsenceDay.setText(String.format("%.2f", item.getDays()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return view;
	}

}
